/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AbstractRequestManagerView.java
 *
 * Created on Aug 20, 2011, 10:56:11 AM
 */
package requestManager;

import app.Global;
import app.MyTable;
import app.MyView;
import entities.Department;
import java.awt.Component;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JScrollPane;

/**
 *
 * @author yoshi
 */
public abstract class AbstractRequestManager extends javax.swing.JPanel implements MyView {

    private long departmentId;
    public MyTable table = new MyTable();
    protected JScrollPane tableScroll;
    private RequestManagerModel myModel = null;
    private RequestManagerController myController = null;

    @Override
    public RequestManagerController getMyController() {
        return myController;
    }

    @Override
    public RequestManagerModel getMyModel() {
        return myModel;
    }

    public MyTable getMyTable() {
        return table;
    }

    public AbstractRequestManager(long departmentId) {
        this(departmentId, new RequestManagerModel(departmentId), new RequestManagerController(departmentId), true);
    }

    protected AbstractRequestManager(long departmentId, RequestManagerModel myModel, RequestManagerController myController, boolean autoInit) {
        initComponents();
        final AbstractRequestManager self = this;

        this.departmentId = departmentId;
        this.myModel = (RequestManagerModel) myModel;
        this.myController = (RequestManagerController) myController;
        myController.setMyView(this);
        
        if(Global.getAuth().isManager() || Global.getAuth().isAdmin()){
            cbbDepartment.setModel(new DefaultComboBoxModel(Global.getDepartments().values().toArray()));
        }else{
            cbbDepartment.setModel(new DefaultComboBoxModel(new Object[] {Global.getAuth().getDepartment()}));
            cbbDepartment.setEnabled(false);
        }
        
        cbbDepartment.addItemListener(new java.awt.event.ItemListener() {
            @Override
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                if (evt.getStateChange() == 1) {
                    long departmentId = ((Department)evt.getItem()).getId();
                    self.setDepartmentId(departmentId);
                    self.refresh();
                }
            }
        });
        
        cbbDepartment.setRenderer(new DefaultListCellRenderer(){
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                this.setText(((Department)value).getName());
                return c;
            }
        });

        if (autoInit) {
            this.initModel();
            this.initTable();
        }

        tableScroll = new JScrollPane(table);
        tableScroll.setSize(870, 450);
        contentPanel.add(tableScroll);
        
        Global.getFrame().setTitle("Request Manager");
    }

    protected abstract void initModel();

    protected abstract void initTable();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        headPanel = new javax.swing.JPanel();
        cbbDepartment = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        contentPanel = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        headPanel.setPreferredSize(new java.awt.Dimension(880, 60));

        cbbDepartment.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Department:");

        javax.swing.GroupLayout headPanelLayout = new javax.swing.GroupLayout(headPanel);
        headPanel.setLayout(headPanelLayout);
        headPanelLayout.setHorizontalGroup(
            headPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, headPanelLayout.createSequentialGroup()
                .addContainerGap(200, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbbDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );
        headPanelLayout.setVerticalGroup(
            headPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(headPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbbDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        add(headPanel, java.awt.BorderLayout.PAGE_START);

        javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
        contentPanel.setLayout(contentPanelLayout);
        contentPanelLayout.setHorizontalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        contentPanelLayout.setVerticalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 240, Short.MAX_VALUE)
        );

        add(contentPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbbDepartment;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JPanel headPanel;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void refresh() {
        this.getMyModel().clear();
        this.initModel();
        this.initTable();
        tableScroll.setViewportView(table);
    }

    private void setDepartmentId(long departmentId) {
        this.departmentId = departmentId;
    }
}
